package de.bht.fb6.cg1.exercise3.implement;
import de.bht.fb6.cg1.exercise3.Ring;

/**
 * integer ring implementation
 */
public class IntRing implements Ring<Integer> {

	/**
	 * internal value
	 */
	protected Integer number;

	/**
	 * create a new ring from a given integer
	 * @param n initial number to set
	 */
	public IntRing(final Integer n) {
		number = n;
	}

	/**
	 * create a new ring initialised with 0
	 */
	public IntRing() {
		this(0);
	}

	/**
	 * provides + operation
	 * @param value to add
	 * @return result
	 */
	@Override
	public Ring<Integer> add(final Ring<Integer> n) {
		return new IntRing(number + n.get());
	}

	/**
	 * get the current value
	 */
	@Override
	public Integer get() {
		return number;
	}

	/**
	 * provides * operation
	 * @param n value to multiply
	 * @return result
	 */
	@Override
	public Ring<Integer> times(final Ring<Integer> n) {
		return new IntRing(number * n.get());
	}

	/**
	 * get the one element
	 * @return 1
	 */
	@Override
	public Ring<Integer> one() {
		return new IntRing(1);
	}

	/**
	 * set the ring to a number
	 * @param n new number - not null
	 */
	@Override
	public void set(final Integer n) {
		number = n;
	}

	/**
	 * provides - operation
	 * @param n value to subtract
	 * @return result
	 */
	@Override
	public Ring<Integer> sub(final Ring<Integer> n) {
		return new IntRing(number - n.get());
	}

	/**
	 * provides the 1 element
	 * @return 1
	 */
	@Override
	public Ring<Integer> zero() {
		return new IntRing(0);
	}

	/**
	 * creates a numeric hashcode
	 * @return numeric hashcode
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		return result;
	}

	/**
	 * compares the obj to another obj
	 * @param obj to compare with
	 * @return true if obj equals this
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IntRing other = (IntRing) obj;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		return true;
	}
}
